<template>
  <div>
    <h3>商品列表</h3>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- card 卡片区 -->
    <el-card>
      <!-- 搜索区 -->
      <el-row :gutter="20" class="mr-bottom">
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            v-model="queryInfo.query"
            clearable
            @clear="getGoodsList"
            @change="getGoodsList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getGoodsList"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="goAddPage">添加商品</el-button>
        </el-col>
      </el-row>

      <!-- 表格区域 -->
      <el-table :data="goodsList" style="width: 100%" stripe border>
        <el-table-column type="index" label="#"> </el-table-column>
        <el-table-column prop="goods_name" label="商品名称"> </el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)" width="100px">
        </el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="100px">
        </el-table-column>
        <el-table-column prop="add_time" label="创建时间" width="130px">
          <template slot-scope="scope">
            {{ scope.row.add_time | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="130px">
          <!-- 点击按钮触发事件时传入当前行的id -->
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
            ></el-button>
            <el-button
              type="primary"
              icon="el-icon-delete"
              size="mini"
              @click="removeGoods(scope.row.goods_id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区 -->
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[3, 5, 10, 15]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 查询参数
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 5
      },
      // 总共商品条数
      total: 0,
      // 保存商品数据
      goodsList: []
    }
  },
  created() {
    // 更新商品数据列表
    this.getGoodsList()
  },
  methods: {
    // 更新商品数据列表
    async getGoodsList() {
      // 向服务器查询商品列表
      const { data: res } = await this.$http.get('goods', {
        params: this.queryInfo
      })
      if (res.meta.status !== 200) {
        return this.$message.error('获取数据失败')
      }
      this.goodsList = res.data.goods
      this.total = res.data.total
    },
    // 当前页显示条数改变时触发
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      // 更新商品数据列表
      this.getGoodsList()
    },
    // 当前第几页改变时触发
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage
      // 更新商品数据列表
      this.getGoodsList()
    },
    // 点击移除商品按钮触发移除函数
    async removeGoods(goodsId) {
      const isConfirm = await this.$confirm(
        '此操作将永久删除该商品, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch(error => error)
      if (isConfirm !== 'confirm') {
        return this.$message.info('取消了删除')
      }
      // 点击了确认 可以发送网络请求删除商品,参数是（goodsId）
      const { data: res } = await this.$http.delete('goods/' + goodsId)
      // console.log(res)
      if (res.meta.status !== 200) {
        return this.$message.error('删除商品失败')
      }
      // 否则删除成功
      this.$message.success('删除商品成功')
      // 更新商品数据列表
      this.getGoodsList()
    },
    // 点击添加按钮触发函数执行跳转页面
    goAddPage() {
      this.$router.push('goods/add')
    }
  }
}
</script>

<style></style>
